﻿using DG.Tweening;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public class NoticeTipsItem : MonoBehaviour
{
	public class NoticeTipsEvent : UnityEvent { }
	private readonly NoticeTipsEvent _onShow = new NoticeTipsEvent();
	private readonly NoticeTipsEvent _onHide = new NoticeTipsEvent();

	private Text _uiLabel;
	private bool _isShow;
	private const float Speed = 100;
	private NoticeTipsWin _tipsWin;

	void Awake()
	{
		_tipsWin = GetComponentInParent<NoticeTipsWin>();
		_uiLabel = GetComponent<Text>();
		_isShow = false;
	}

	void Start()
	{
		float time = (_tipsWin.BgWidth + _uiLabel.preferredWidth) / Speed;
		_uiLabel.transform.DOLocalMoveX(-(_tipsWin.BgWidth / 2f + _uiLabel.preferredWidth / 2f), time).SetEase(Ease.Linear).OnUpdate(OnTweenUpdate).OnComplete(OnTweenComplete);
	}

	private void OnTweenUpdate()
	{
		if (!_isShow && transform.localPosition.x < (_tipsWin.BgWidth / 2f - _uiLabel.preferredWidth / 2f-80))
		{
			_onShow.Invoke();
			_isShow = true;
		}
	}

	private void OnTweenComplete()
	{
		GameTools.Destroy(_uiLabel.gameObject);
		_onHide.Invoke();
	}

	public void SetInfo(string tips)
	{
		_uiLabel.text = tips;
		transform.localPosition = Vector3.right * (_tipsWin.BgWidth / 2f + _uiLabel.preferredWidth / 2f);
	}

	public NoticeTipsEvent OnShow { get { return _onShow; } }

	public NoticeTipsEvent OnHide { get { return _onHide; } }
}
